package controller

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"strings"
	"ucenter/app/model"
	"ucenter/app/service"
	"ucenter/app/validator"
)

/**
 * 获取用户基本信息
 * param: *gin.Context c
 */
func GetUserInfo(c *gin.Context) {
	uid := c.GetInt64("UID")
	if uid == 0 {
		return
	}
	resKey, userInfo, balanceInfo := service.GetUserService().GetUserBaseInfo(uid)
	if resKey != "" {
		c.JSON(500, &map[string]interface{}{"code": "500", "msg": resKey})
		return
	}
	c.JSON(200, &map[string]interface{}{
		"code":      "200",
		"user_info": userInfo,
		"balance":   balanceInfo,
	})
	return
}

func GetUserBalance(c *gin.Context) {
	uid := c.GetInt64("UID")
	if uid == 0 {
		return
	}
	req := new(model.GetUserBalance)
	req.Uid = append(req.Uid, uid)
	data, err := service.GetBalanceService().GetBalance(req)
	if err != nil {
		c.JSON(500, &map[string]interface{}{"code": "500", "msg": "DB_ERROR"})
		return
	}
	userBalance := model.UserBalance{}
	if len(data) > 0 {
		userBalance = data[0]
	}
	c.JSON(200, &map[string]interface{}{
		"code": "200",
		"data": userBalance,
	})
	return
}

func GetSimpleUserInfo(c *gin.Context) {
	uid, err := strconv.ParseInt(c.Param("uid"), 10, 64)
	if uid == 0 || err != nil {
		c.JSON(500, &map[string]interface{}{"code": "500", "msg": "PARAM_ERR"})
		return
	}
	resKey, userInfo, _ := service.GetUserService().GetSimpleUserInfo(uid)
	if resKey != "" {
		c.JSON(500, &map[string]interface{}{"code": "500", "msg": resKey})
		return
	}
	c.JSON(200, &map[string]interface{}{
		"code": "200",
		"data": map[string]interface{}{
			"id":        userInfo.Id,
			"nick_name": userInfo.NickName,
			"avatar":    userInfo.Avatar,
		},
	})
	return
}

func BatchGetSimpleUserInfo(c *gin.Context) {
	var (
		uidList     []int64
		uidStrSlice []string
		err         error
	)

	uid := c.Param("uid")
	if uid == "" {
		return
	}
	uidStrSlice = strings.Split(uid, ",")
	if err != nil {
		c.JSON(500, &map[string]interface{}{"code": "500", "msg": "UID_ERROR"})
		return
	}
	for _, _uid := range uidStrSlice {
		if _uidInt, err := strconv.ParseInt(_uid, 10, 64); err == nil {
			uidList = append(uidList, _uidInt)
		}
	}
	if len(uidList) <= 0 || len(uidList) > 10 {
		c.JSON(400, &map[string]interface{}{"code": "400", "msg": "UID_TOO_MANY"})
		return
	}
	resKey, _, list := service.GetUserService().GetSimpleUserInfo(uidList)
	if resKey != "" {
		c.JSON(500, &map[string]interface{}{"code": "500", "msg": resKey})
		return
	}
	ret := &map[string]interface{}{
		"code": "200",
	}
	var retList []*map[string]interface{}
	for _, userInfo := range list {
		retList = append(retList, &map[string]interface{}{
			"id":        userInfo.Id,
			"nick_name": userInfo.NickName,
			"avatar":    userInfo.Avatar,
		})
	}
	(*ret)["users"] = retList

	c.JSON(200, ret)
	return
}

func UpdateUserInfo(c *gin.Context) {
	var (
		updateValidator validator.UpdateValidator
		uid             int64
	)
	uid = c.GetInt64("UID")
	if uid == 0 {
		return
	}
	if err := c.ShouldBind(&updateValidator); err != nil {
		//c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		c.JSON(http.StatusBadRequest, &map[string]interface{}{"code": "401", "msg": err.Error()})
		return
	}
	if vs, msg := updateValidator.Valid(); !vs {
		c.JSON(http.StatusBadRequest, &map[string]interface{}{"code": "401", "msg": msg})
		return
	}
	err := service.GetUserService().UpdateInfo(c, &model.User{
		Id:      uid,
		Name:    updateValidator.Name,
		BankNo:  updateValidator.BankNo,
		PayTM:   updateValidator.PayTm,
		Email:   updateValidator.Email,
		Address: updateValidator.Address,
	})
	if err != nil {
		c.JSON(500, &map[string]interface{}{"code": "500", "msg": "Update Fail"})
		return
	}

	c.JSON(200, &map[string]interface{}{"code": "200", "msg": "success"})
	return
}
